/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testing_class;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import stendcontrol.AbstractMessage;
import stendcontrol.Configurator;
import stendcontrol.DBServer;
import stendcontrol.Experiment;
import stendcontrol.FileServer;
import stendcontrol.StatusMessage;
import stendcontrol.StendLogger;

/**
 *
 * @author Asko
 */
public class ServerTest {

    private static String invalid_tokens = "\\|/:?:\"<>*";
    private final String class_name = "com.mysql.jdbc.Driver";
    private Configurator conf = Configurator.getConf();
    private Connection con;
    private Statement statement;
    private String current_directory = "";
    private String separator = File.separator;
    private Properties prop ;
    private File cache ;
    
    public static void main(String[] args) throws IOException {

        File file = new File("E:\\");
        //file.createNewFile();
        System.out.println("Is directory ? " + file.isDirectory());
        System.out.println("Is exists? " + file.exists());

        File[] files = file.listFiles();
        for (File f : files) {
            System.out.println("File name: " + f.getName());
        }

    }

    public String[] getExpNames() {

        File file = new File(current_directory + separator);
        File[] files = file.listFiles();
        String[] res = new String[files.length];
        for (int i = 0; i < res.length; i++) {
            res[i] = files[i].getName();
        }
        return res;
    }
    
    public String[] getExpNames(int shit){
        String[] str = null;
        File curFile = new File(current_directory+separator);
        try {
            prop.load(new FileInputStream(cache));
            Set<Object> set = prop.keySet();
            if (set != null) {
                str = new String[set.size()];
                Iterator it = set.iterator();
                int i = 0;
                while (it.hasNext()) {
                    str[i] = (String) it.next();
                    i++;
                }
            } else {
                StendLogger.log(StendLogger.MSG_ERROR, "cache file is empty");
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FileServer.class.getName())
                    .log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(FileServer.class.getName())
                    .log(Level.SEVERE, null, ex);
        }
        return str;
    }

    public static String convertName(String exp_name) {
        StringTokenizer st = new StringTokenizer(exp_name, invalid_tokens);
        String res = "";
        while (st.hasMoreTokens()) {

            res += st.nextToken();
        }

        return res;
    }

    public void createConnection() {
        try {
            Class.forName(class_name);
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/"
                    + "test_db", "root",
                    "1618144");
            statement = con.createStatement();
            if (con != null) {
                System.out.println("Connection  created successfully");
            } else {
                StendLogger.log(StendLogger.MSG_ERROR, "Failed to connect to "
                        + "database");
            }

        } catch (SQLException e) {
            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            System.out.println("Where is your MySQL JDBC Driver?");
            e.printStackTrace();
        }

    }

    public String[] getNamesExp() {
        String query = "Select exp_name from exp_data";
        ArrayList<String> rs = new ArrayList<String>();
        String res[] = null;
        try {
            ResultSet set = statement.executeQuery(query);
            while (set.next()) {
                rs.add(set.getString(1));

            }
            res = new String[rs.size()];
            for (int i = 0; i < res.length; i++) {
                res[i] = (String) rs.get(i);

            }
        } catch (SQLException ex) {
            Logger.getLogger(DBServer.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
        return res;
    }
}
